import numpy as np
from PIL import Image
import scipy
import scipy.io
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
I_L = Image.open("data/im0.ppm")
I_R = Image.open("data/im8.ppm")
I_L_num = np.array(I_L)
I_R_num = np.array(I_R)
Now lets plot both the images
plt.imshow(I_R_num)
<matplotlib.image.AxesImage at 0x7ff618039be0>
plt.imshow(I_L_num)
<matplotlib.image.AxesImage at 0x7ff648054f70>
Lets develop the disparity matrix
row_val = 381
col_val = 390
pix_range = 40
# Since we are trying to find the which among the 40 pixels are the closest
D_M = np.zeros((row_val,col_val))
for each_index_1,row_pix in enumerate(I_L_num):
row_pix_len = len(row_pix)
for each_index_2 in range(row_pix_len):
if col_val>each_index_2:
right_split = I_R_num[each_index_1,each_index_2,:]
left_split = I_L_num[each_index_1,each_index_2:each_index_2+pix_range,:]
dist_value = np.linalg.norm(right_split - left_split,axis = 1)
D_M[each_index_1,each_index_2] = np.argmax(dist_value)
D_M_flat = D_M.reshape(-1,1)
plt.figure(figsize = (7,5),dpi = 200)
sns.histplot(D_M_flat, bins = 39, color = 'navy')
plt.show()
def gaussian_dist(mean,sd,data):
diff = ((data - mean) / sd)**2
norm = 1 / (sd*np.sqrt(2*np.pi))
pdf = norm*np.exp(-1/2*(diff))
return pdf
def get_frame(u, input_matrix):
disp = ['Disparity Data']
mem_data = np.argmax(u, axis = 1)
table = pd.DataFrame(input_matrix)
table.columns = disp
table['Cluster'] = mem_data
return table
def sd_comp(K, sd, mean, input_matrix,u):
for each in range(K):
term_1 = u[:,each].reshape(-1,1)
term_2 = input_matrix - mean[each]
term_3 = input_matrix - mean[each]
term_4 = np.sum(u[:,each])
inner_term = np.sum(term_1*((term_2)*(term_3)))/term_4
sd[each] = np.sqrt(inner_term)
return sd
def GMM_func(input_matrix, K, clust_1, mean, sd, x_clus, u, iterate, rate_1):
for each_1 in range(iterate):
for each_2 in range(K):
x_clus[:,each_2] = gaussian_dist(mean[each_2], sd[each_2], input_matrix).reshape(1,-1)
x_clus[x_clus==0] = rate_1
val_mul = clust_1*x_clus
x_upd = np.sum(val_mul, axis=1)
denom = x_upd.reshape(-1,1)
u = val_mul/denom
input_reshape = input_matrix.reshape(-1,1)
term_1 = u * input_reshape
u_sum = np.sum(u, axis = 0)
mean = np.sum(term_1, axis = 0)/u_sum
clust_1 = np.mean(u, axis=0)
sd = sd_comp(K, sd, mean, input_matrix,u)
table = get_frame(u,input_matrix)
return table, mean, sd
def get_maps(table, D_M):
clus_map = np.array(table['Cluster'])
clus_map = clus_map.reshape(D_M.shape)
output_map = np.array(table['Cluster'], dtype=float)
for i in range(4):
output_map[output_map == i] = means[i]
output_map = output_map.reshape(D_M.shape)
return output_map,clus_map
def sim_result(each_1, each_2, data_clust):
con_1 = clus_hist.shape[0]-1
con_2 = clus_hist.shape[1]-1
if each_1 < 0 or each_2 < 0 or each_1 > con_1 or each_2 > con_2 or clus_hist[each_1,each_2] == data_clust:
return 1
val = 5
sig = 0.5
if clus_hist[each_1,each_2] == data_clust:
val = 0
term_1 = sig * sig * 2
term_2 = val * val
final = np.exp(-(term_2/(term_1)))
return final
def preset(num_1,num_2,cluster):
output = 1
range_num = [-1,0,1]
for each_1 in range_num:
for each_2 in range_num:
term_1 = num_1+each_1
term_2 = num_2+each_2
output *= sim_result(term_1,term_2, cluster)
return output
final = []
K = 4
num = [1/K]*K
clust_1 = np.array(num)
iterate = 500
rate_1 = 0.00005
input_size = np.size(D_M_flat)
mean = np.random.uniform(10, 50, K)
sd = np.random.uniform(1, 5, K)
x_clus = np.zeros((input_size, K))
u = np.zeros((input_size, K))
table, means, sds = GMM_func(D_M_flat, K, clust_1, mean, sd, x_clus, u, iterate, rate_1)
output_map,clus_map = get_maps(table, D_M)
plt.imshow(output_map,cmap="gray",aspect = 'auto')
plt.show()
clus_hist = np.array(clus_map)
output_smooth = np.zeros(D_M.shape)
clus_smooth = np.array(clus_map)
D_M_shape_row = D_M.shape[0]
D_M_shape_col = D_M.shape[1]
iterate = 30
random_arrange = np.arange(0, 4)
gauss_range = 4
for main_each in range(iterate):
for each_1 in range(D_M_shape_row):
for each_2 in range(D_M_shape_col):
post = np.zeros(4)
clust = clus_map[each_1,each_2]
for each_3 in range(gauss_range):
pdf = gaussian_dist(means[each_3], sds[each_3], output_map[each_1,each_2])
post[each_3] = pdf * preset(each_1,each_2,each_3)
post_sum = np.sum(post)
post = post/post_sum
inter_lab = np.random.choice(random_arrange, p=post)
output_smooth[each_1,each_2] = means[inter_lab]
clus_smooth[each_1,each_2] = inter_lab
clus_hist = np.array(clus_smooth)
final.append(output_smooth)
final_array = np.array(final)
final_output_image = scipy.stats.mode(final_array)[0][0]
plt.title('Result after using smoothening')
plt.imshow(final_output_image,cmap="gray")
plt.show()
import IPython.display as ipd
import librosa
note_1, sr_1 = librosa.load('data/trs.wav',sr=None)
ipd.Audio(np.real(note_1),rate=sr_1)
note_2, sr_2 = librosa.load('data/trn.wav',sr=None)
ipd.Audio(np.real(note_2),rate=sr_2)
note_3, sr_3 = librosa.load('./data/tex.wav',sr=None)
ipd.Audio(np.real(note_3),rate=sr_3)
note_4, sr_4 = librosa.load('./data/tes.wav',sr=None)
ipd.Audio(np.real(note_4),rate=sr_4)
Lets take the STFT of all the signals
note_1_stft = librosa.stft(note_1, n_fft=1024, hop_length= 1024//2, window='hann')
note_2_stft = librosa.stft(note_2, n_fft=1024, hop_length= 1024//2, window='hann')
note_3_stft = librosa.stft(note_3, n_fft=1024, hop_length= 1024//2, window='hann')
note_4_stft = librosa.stft(note_4, n_fft=1024, hop_length= 1024//2, window='hann')
K_s = 25
K_n = 25
rate = 0.00001
def iterate_1(data_b, data_theta, input_data, data_theta_first, data_b_first, iter_val, rate):
for i in range(iter_val):
part_1_1 = np.dot(data_b,data_theta) + rate
part_1_2 = np.divide(input_data,part_1_1)
part_1_3 = np.dot(part_1_2, data_theta.T)
data_b = np.multiply(data_b,part_1_3)
part_2_1 = np.dot(data_b,data_theta) + rate
part_2_2 = np.divide(input_data,part_2_1)
part_2_3 = np.dot(data_b.T,part_2_2)
data_theta = np.multiply(data_theta,part_2_3)
b_dot = np.dot(data_b_first,data_b) + rate
data_b = data_b/b_dot
theta_dot = np.dot(data_theta_first,data_theta) + rate
data_theta = data_theta/theta_dot
return (data_b,data_theta)
def iterate_2(data_b, data_theta, input_data, data_theta_first, iter_val, rate):
for i in range(iter_val):
part_1 = np.dot(data_b,data_theta) + rate
part_2 = np.divide(input_data,part_1)
part_3 = np.dot(data_b.T,part_2)
data_theta = np.multiply(data_theta,part_3)
theta_dot = np.dot(data_theta_first,data_theta) + rate
data_theta = data_theta/theta_dot
return data_theta
def b_func(index, input_data, rate):
shape_data_row = input_data.shape[0]
shape_data_col = input_data.shape[1]
data_b = np.random.randn(shape_data_row, index)
data_theta = np.random.randn(index,shape_data_col)
b_shape_row = data_b.shape[0]
theta_dhape_row = data_theta.shape[0]
data_b_first = np.ones((b_shape_row,b_shape_row))
data_theta_first = np.ones((theta_dhape_row,theta_dhape_row))
b_dot = np.dot(data_b_first,data_b) + rate
data_b = data_b/b_dot
theta_dot = np.dot(data_theta_first,data_theta) + rate
data_theta = data_theta/theta_dot
iter_val = 500
updated_data = iterate_1(data_b, data_theta, input_data, data_theta_first, data_b_first, iter_val, rate)
return updated_data
abs_note_1_stft = abs(note_1_stft)
abs_note_2_stft = abs(note_2_stft)
abs_note_3_stft = abs(note_3_stft)
data_b_1, data_theta_1 = b_func(K_s, abs(abs_note_1_stft), rate)
data_b_2, data_theta_2 = b_func(K_n, abs(abs_note_2_stft), rate)
data_b_3 = np.hstack((data_b_1,data_b_2))
def func_theta(input_data, val_k, data_b, rate):
input_shape_col = input_data.shape[1]
data_theta = np.random.randn(val_k,input_shape_col)
theta_shape_row = data_theta.shape[0]
data_theta_first = np.ones((theta_shape_row,theta_shape_row))
theta_dot = np.dot(data_theta_first,data_theta)
data_theta = data_theta/theta_dot
iter_val = 500
new_theta = iterate_2(data_b, data_theta, input_data, data_theta_first, iter_val, rate)
return new_theta
val_k = K_s+K_n
data_theta_3 = func_theta(abs_note_3_stft, val_k, data_b_3, rate)
theta_3_splice = data_theta_3[:K_s,:]
matrix_data = np.dot(data_b_1,theta_3_splice)
theta_dot = np.dot(data_b_3,data_theta_3) + rate
s_1 = matrix_data/theta_dot
s_final = np.multiply(s_1,note_4_stft)
s_recovered = librosa.istft(s_final, n_fft = 1024, hop_length= 1024//2, window='hann')
ipd.Audio(np.real(s_recovered),rate=sr_3)
note_diff = note_4 - s_recovered
note_dot = np.dot(note_4.T,note_4)
diff_dot = np.dot(note_diff.T,note_diff)
rate_data = note_dot/diff_dot
SNR = np.log10(rate_data)*10
print(SNR)
9.849370439380763
data = scipy.io.loadmat('data/twitter.mat')
x_tr = data['Xtr']
x_te = data['Xte']
y_tr_mat = data['YtrMat']
y_te_mat = data['YteMat']
topic = np.random.randn(891, 50)
weight = np.random.randn(50, 773)
weight_psli = np.random.rand(50,193)
b_val = np.random.uniform(0, 5, (3,1))
rate_1 = 0.0001
rate_2 = 0.005
splice_num = 3
for each_iter in range(500):
val = np.matmul(topic,weight)
val[val == 0] = rate_1
weight_trans = weight.T
topic_trans = topic.T
x_tr_dot = np.dot((x_tr/val), weight_trans)
topic = x_tr_dot * topic
topic_shape_row = topic.shape[0]
topic_ones = np.ones([topic_shape_row, topic_shape_row])
topic_dot = np.dot(topic_ones,topic)
topic = topic / topic_dot
val = np.dot(topic,weight)
val[val == 0] = rate_1
new_dot = np.dot(topic_trans, (x_tr/val))
weight = new_dot * weight
weight_shape_row = weight.shape[0]
weight_ones = np.ones([weight_shape_row, weight_shape_row])
weight_dot = np.dot(weight_ones,weight)
weight = weight / weight_dot
for i in range(1000):
val = np.dot(topic, weight_psli)
val[val == 0] = rate_2
topic_dot = np.dot(topic.T, (x_te/val))
weight_psli = topic_dot * weight_psli
weight_psli_shape_row = weight_psli.shape[0]
weight_psli_ones = np.ones([weight_psli_shape_row, weight_psli_shape_row])
weight_psli_dot = np.dot(weight_psli_ones, weight_psli)
weight_psli = weight_psli / weight_psli_dot
w_val = np.random.uniform(0, 5, (splice_num, weight_shape_row))
output = []
for i in range(1000):
y_tr_shape_col = y_tr_mat.shape[1]
weight_com = np.dot(w_val, weight)
weight_tot = weight_com + b_val
y_inter = np.exp(weight_tot)
y_sum = np.sum(y_inter, axis=0)
y_inter = y_inter / y_sum.reshape(1,-1)
y_log = np.log(y_inter)
y_mul = y_tr_mat * y_log
loss_val = -np.sum(y_mul)
y_diff = y_inter - y_tr_mat
w_d = np.dot((y_diff), weight.T)
y_tr_ones = np.ones([y_tr_shape_col,1])
b_d = np.dot((y_diff), y_tr_ones)
w_val = w_val - (rate_2 * w_d)
b_val = b_val - (rate_2 * b_d)
output.append(loss_val)
z_dot_output = np.dot(w_val, weight_psli) + b_val
y_inter_final = np.exp(z_dot_output)
y_inter_final_sum = np.sum(y_inter_final, axis=0)
y_inter_final = y_inter_final / y_inter_final_sum.reshape(1,-1)
acc_argmax = np.argmax(y_inter, axis=0) == np.argmax(y_tr_mat, axis=0)
train_accuracy = np.sum(acc_argmax) / y_tr_shape_col
test_accuracy = np.sum(np.argmax(y_inter_final, axis=0) == np.argmax(y_te_mat, axis=0)) / y_te_mat.shape[1]
plt.figure(figsize = (10,8))
plt.title('The plot for convergence')
plt.plot(output)
plt.show()
train_result = np.round(train_accuracy,3)
test_result = np.round(test_accuracy,3)
print("The train Accuracy :",train_result)
print("The test Accuracy :",test_result)
The train Accuracy : 0.617 The test Accuracy : 0.56